import { _decorator, Button, Component, Game, Label, Node, Sprite } from 'cc';
import { UIComponent } from 'db://assets/scripts/platform/ui/UIComponent';
import { bind } from '../../../platform/ui/UIDecorator';
import { Enum_GameIds, Game_Level_Names, Game_Levels } from '../../common/enums';
import { PlayerData } from '../../data/player/PlayerData';
import UIMgr from '../../../platform/ui/UIMgr';
import { GameView } from '../game/GameView';
import { Game2048PlayView } from '../2048/Game2048PlayView';
import { Tb_GameList } from '../../tb/Tb_GameList';
import { GlobalEventMgr } from '../../../platform/event/GlobalEventMgr';
import { GlobalEventTyp } from '../../common/GlobalEventType';
import { GameWuziqiPlayView } from '../wuziqi/GameWuziqiPlayView';

const { ccclass, property } = _decorator;
//mvc
@ccclass('GameLevel')
export class GameLevel extends UIComponent {

    @bind(Label)
    played_num:Label
    @bind(Node)
    lock:Node

    @bind(Label)
    level_name:Label

    level:number = Game_Levels.Simple
    init(level:number){
        this.level = level
        this.level_name.string = Game_Level_Names[this.level]
        //更新是否解锁
        this.updateLockAndNumStatus()
        //TODO 跟新玩家玩过的场数

        this.node.on(Button.EventType.CLICK,this.onLevelClick,this)
        GlobalEventMgr.ins.addNotice(GlobalEventTyp.GameLevelPlayNumChange,this,this.updateLockAndNumStatus)
    }




    updateLockAndNumStatus(){
        let view = this.ownerView as GameView
        let cfg = view.cfg
        this.played_num.string = "0"
        if(cfg.id===1){
            this.game2048LockAndNumStatus()
        }else if(cfg.id===2){//五子棋
            this.gameWuziqiLockAndNumStatus()
        }

    }
    gameWuziqiLockAndNumStatus() {

    }
    game2048LockAndNumStatus() {
        //TODO 
        let data = PlayerData.ins.game2048
        let levelInfo = data.levelInfos.get(this.level)
        this.played_num.string = levelInfo.playedNum+""
        if(this.level==Game_Levels.Simple){//简单模式不锁定
            this.lock.active = false    
            return
        }
        let preLevelInfo = data.levelInfos.get(this.level-1)
        if(preLevelInfo.passed){//从玩家数据中检测这个难度是否解锁(玩固定局数[枚举必须合成出几个数字]或通关)
            this.lock.active = false
            return
        }
        if(preLevelInfo.playedNum<10){//局数应该配置
            this.lock.active = true
            return
        }
        this.lock.active = true
    }

    onLevelClick(){
        console.log("点击难度:"+this.level);
        //TODO 根据解锁情况打开游戏界面
        let cfg = (this.ownerView as GameView).cfg
        if(!this.isLevelOpen(cfg,this.level)){
            UIMgr.ins.showTips("当前难度未解锁，请先完成前一个难度")
            return
        }
        //检查难度是否开启
        if(cfg.id===Enum_GameIds.Game2048){
            UIMgr.ins.showUI(Game2048PlayView,null,cfg,this.level)
            return
        }
        if(cfg.id===2){
            UIMgr.ins.showUI(GameWuziqiPlayView,null,cfg,this.level)
            return
        }
    }
    isLevelOpen(cfg: Tb_GameList, level: number):boolean {
        if(level===Game_Levels.Simple){//简单模式
         return true;       
        }
        let levelInfo = null
        if(cfg.id ===Enum_GameIds.Game2048){//2048
            let data = PlayerData.ins.game2048
            levelInfo = data.levelInfos.get(level-1)

        }else if(cfg.id ===Enum_GameIds.GameWuziqi){//五子棋
            let data = PlayerData.ins.gameWuziqi
            levelInfo = data.levelInfos.get(level-1)
        }
        if(levelInfo && levelInfo.passed){
            return true
        }
        return false
    }
}




